package com.efrei.uchat.client.gui.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

public class LoginTab extends JPanel {

	private static final long serialVersionUID = 7961475821812523961L;
	
	private JTextPane txtpnHost;
	private JTextPane txtpnPort;
	private JTextPane txtpnNickname;
	
	private JButton btnSubmit;
	
	private JTextField textPseudo;
	private JTextField textHost;
	private JTextField textPort;
	
	public LoginTab() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		this.txtpnNickname = new JTextPane();
		this.txtpnNickname.setBackground(UIManager.getColor("SplitPane.background"));
		this.txtpnNickname.setText("Nickname :");
		this.txtpnNickname.setAlignmentX(JTextField.CENTER_ALIGNMENT);
		this.txtpnNickname.setAlignmentY(JTextField.CENTER_ALIGNMENT);
		GridBagConstraints gbc_txtpnNickname = new GridBagConstraints();
		gbc_txtpnNickname.anchor = GridBagConstraints.EAST;
		gbc_txtpnNickname.insets = new Insets(0, 0, 5, 5);
		gbc_txtpnNickname.gridx = 0;
		gbc_txtpnNickname.gridy = 0;
		add(this.txtpnNickname, gbc_txtpnNickname);
		
		this.textPseudo = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.anchor = GridBagConstraints.WEST;
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		add(this.textPseudo, gbc_textField);
		this.textPseudo.setColumns(10);
		
		this.txtpnHost = new JTextPane();
		this.txtpnHost.setBackground(UIManager.getColor("TabbedPane.background"));
		this.txtpnHost.setText("Host :");
		this.txtpnHost.setAlignmentX(JTextField.CENTER_ALIGNMENT);
		this.txtpnHost.setAlignmentY(JTextField.CENTER_ALIGNMENT);
		GridBagConstraints gbc_txtpnHost = new GridBagConstraints();
		gbc_txtpnHost.anchor = GridBagConstraints.EAST;
		gbc_txtpnHost.insets = new Insets(0, 0, 5, 5);
		gbc_txtpnHost.gridx = 0;
		gbc_txtpnHost.gridy = 1;
		add(this.txtpnHost, gbc_txtpnHost);
		
		this.textHost = new JTextField();
		GridBagConstraints gbc_textField_2 = new GridBagConstraints();
		gbc_textField_2.anchor = GridBagConstraints.WEST;
		gbc_textField_2.insets = new Insets(0, 0, 5, 0);
		gbc_textField_2.gridx = 1;
		gbc_textField_2.gridy = 1;
		add(this.textHost, gbc_textField_2);
		this.textHost.setColumns(10);
		
		this.txtpnPort = new JTextPane();
		this.txtpnPort.setBackground(UIManager.getColor("TabbedPane.background"));
		this.txtpnPort.setText("Port : ");
		this.txtpnPort.setAlignmentX(JTextField.CENTER_ALIGNMENT);
		this.txtpnPort.setAlignmentY(JTextField.CENTER_ALIGNMENT);
		GridBagConstraints gbc_txtpnPort = new GridBagConstraints();
		gbc_txtpnPort.anchor = GridBagConstraints.EAST;
		gbc_txtpnPort.insets = new Insets(0, 0, 5, 5);
		gbc_txtpnPort.gridx = 0;
		gbc_txtpnPort.gridy = 2;
		add(this.txtpnPort, gbc_txtpnPort);
		
		this.textPort = new JTextField();
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.anchor = GridBagConstraints.WEST;
		gbc_textField_1.insets = new Insets(0, 0, 5, 0);
		gbc_textField_1.gridx = 1;
		gbc_textField_1.gridy = 2;
		add(this.textPort, gbc_textField_1);
		this.textPort.setColumns(10);
		
		this.btnSubmit = new JButton("Submit");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.anchor = GridBagConstraints.CENTER;
		gbc_btnNewButton.gridwidth = 2;
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 3;
		add(this.btnSubmit, gbc_btnNewButton);
	}
	
	public LoginTab registerController(ActionListener controller) {
		btnSubmit.addActionListener(controller);
		return this;
	}

	/**
	 * add controller to all components which waiting an actionListener
	 * @param controller
	 * @return LoginTab instance
	 */
	public LoginTab addController(ActionListener controller) {
		btnSubmit.addActionListener(controller);
		return this;
	}
	
	public JButton getBtnSubmit() {
		return btnSubmit;
	}
	
	public JTextField getTextPseudo() {
		return textPseudo;
	}

	public JTextField getTextHost() {
		return textHost;
	}

	public JTextField getTextPort() {
		return textPort;
	}
}
